import 'dart:core';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/chat/employee_detail.dart';
import 'package:sui_shou_pai/chat/entity/department_model.dart';
import 'package:sui_shou_pai/module/color.dart';
import 'package:sui_shou_pai/module/effect.dart';
import 'package:sui_shou_pai/network/http_config.dart';
import 'package:sui_shou_pai/network/http_request.dart';

class EmployeeList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return EmployeeListState();
  }
}

class EmployeeListState extends State<EmployeeList> {
  Map<String, dynamic> employeeMap = {};
  List<DepartmentModel> department = [];

  @override
  void initState() {
    super.initState();
    getEmployeeList();
  }

  Future<void> getEmployeeList() async {
    HttpRequest.request("$BASE_URL/employee/selectDE",
            method: "get", token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        employeeMap = value.data["result"];
        employeeMap.forEach((key, value) {
          setState(() {
            DepartmentModel departmentModel =
                DepartmentModel.formMap(key, value);
            department.add(departmentModel);
          });
        });
      } else {
        Fluttertoast.showToast(msg: "数量加载失败");
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
        itemCount: department.length,
        itemBuilder: (context, index) {
          return createItem(index);
        },
      ),
    );
  }

  List<Widget> widgets(int index) {
    List<Widget> w = [];
    w = List.generate(department[index].employeeList.length, (i) {
      return Column(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            child: Container(
                padding: EdgeInsets.symmetric(vertical: 10, horizontal: 12),
                child: Row(
                  children: [
                    Container(
                      margin: EdgeInsets.only(right: 20),
                      height: 60,
                      width: 60,
                      child: ClipRRect(
                        borderRadius: borderRadiusAll40(),
                        child: Image.network(
                            department[index].employeeList[i].avatar,
                            fit: BoxFit.cover),
                      ),
                    ),
                    Container(
                        height: 60,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Container(
                              child: Text(
                                  department[index].employeeList[i].name,
                                  style: TextStyle(
                                      fontSize: 18,
                                      fontWeight: FontWeight.bold)),
                            ),
                            Container(
                              child: Text(
                                department[index]
                                        .employeeList[i]
                                        .departmentName +
                                    "  " +
                                    department[index].employeeList[i].position,
                                style:
                                    TextStyle(color: Colors.grey, fontSize: 14),
                              ),
                            )
                          ],
                        ))
                  ],
                )),
            onTap: () {
              Navigator.push(
                  context,
                  new MaterialPageRoute(
                      builder: (context) => EmployeeDetailPage(
                          employeeModel: department[index].employeeList[i])));
            },
          ),
          Divider(height: 5.0, color: greyF6F6F6, thickness: 1),
        ],
      );
    });
    return w;
  }

  Widget createItem(int index) {
    return Container(
      margin: EdgeInsets.fromLTRB(12, 12, 12, 0),
      padding: EdgeInsets.symmetric(vertical: 0, horizontal: 12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: borderRadiusAll8(),
        boxShadow: [shadowOffsetY2()],
      ),
      child: ExpansionTile(
          leading: Icon(Icons.person, size: 30),
          title: Text(department[index].employeeList[0].departmentName),
          children: widgets(index)),
    );
  }
}
